<?php

namespace app\admin\model;

use think\Model;

class Module extends Model {

    /**
     * 根据所传菜单ID组，取出对应菜单，并区分级别
     * 即返回[[...],[...],[...,child=>[[],[]]]]
     * @Author   jiating
     * @DateTime 2018-04-13
     * @param    string     $ids   字符串，如1,3,7
     * @param    boolean    $admin 是否为超级管理员
     * @return   arrry            按层级关系返回
     */
    public function toTree($ids,$admin = false) {
        if($admin) {
            $modules = $this->select();
        } else {
            $modules = $this->where('id','in',$ids)->select();
            $pIds = [];
            foreach ($modules as $v) {
                if($v['typeId'] !== 0) {
                    $pIds[] = (string)$v['typeId'];
                }
            }
            $ids = array_unique(array_merge($pIds,explode(',',$ids)));
            $modules = $this->where('id','in',implode(',',$ids))->select();
        }

        $mMap = [];
        foreach ($modules as $v) {
            $mMap[$v['typeId']][] = $v['typeId'] != 0 ? $v : [];
        }
        foreach ($modules as $mk => $mv) {
            if($mv['typeId'] == 0) {
                $modules[$mk]['child'] = isset($mMap[$mv['id']]) ? $mMap[$mv['id']] : [];
            } else {
                unset($modules[$mk]);
            }
        }

        return $modules;
    }

    /**
     * 按父ID查找菜单子项
     *
     * @param string $parentId 父菜单ID
     * @param bool $with_self 是否包括他自己
     * @return array
     */
    public static function getMenu($parentId, $with_self = false)
    {
        // 父节点ID
        $parentId = $parentId;
        $result = Module::where(['typeId' => $parentId])->where('isActive',1)->order('sorting', 'ASC')->select();
        if ($with_self) {
            $result2[] = Module::where(['id' => $parentId])->find();
            $result = array_merge($result2, $result);
        }

        return $result;
    }

    // 取得树形结构的菜单
    public static function getTree($myId, $parent = "", $Level = 1)
    {
        $data = self::getMenu($myId);
        $Level++;

        if (is_array($data)) {
            $ret = NULL;
            foreach ($data as $value) {
                $value = $value->data;
                $id = $value['id'];
                $uriArr = explode('/', $value['path']);
                $app = isset($uriArr['0']) ? $uriArr['0'] : '';
                $controller = isset($uriArr['1']) ? $uriArr['1'] : '';
                $action = isset($uriArr['2']) ? $uriArr['2'] : '';

                $array = array(
                    "app" => $app,
                    "controller" => $controller,
                    "action" => $action,
                    "icon" => $value['icon'],
                    "id" => $id,
                    "name" => $value['name'],
                    "target" => $value['target'],
                    "parent" => $parent,
                    "url" => url($value['path'])
                );

                $ret[$id . $app] = $array;
                $child = self::getTree($value['id'], $id, $Level);
                // 后台管理界面只支持五层，超出的层级的不显示
                if ($child && $Level <= 5) {
                    $ret[$id . $app]['items'] = $child;
                }
            }
            return $ret;
        }

        return false;
    }
}